﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;

namespace SIPO.Library.DataAccess.Administration.Catalog
{
    public partial class recTransactionTypeRepo : ISearchable<recTransactionType>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM master.recTransactionType", column,parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM master.recTransactionType", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM master.recTransactionType", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.recTransactionType", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM master.recTransactionType", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM master.recTransactionType", column, parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recTransactionType> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recTransactionType> tt = new List<recTransactionType>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"      recTransactionType.TransTypeID
                                                , recTransactionType.TransTypeCode
                                                , recTransactionType.TransTypeName
                                                , recTransactionType.TermDay
                                                , recTransactionType.CreatedDate
                                                , recTransactionType.CreatedBy
                                                , recTransactionType.UpdatedDate
                                                , recTransactionType.UpdatedBy
                                                , recTransactionType.BankID
                                                ,recBankAccount.BankName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.recTransactionType LEFT JOIN master.recBankAccount ON recTransactionType.BankID = recBankAccount.BankID ");
                    sbQuery.AppendFormat("    {0}", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recTransactionType t = new recTransactionType(true);                                
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                t.TermDay = dr[recTransactionType.Fields.TermDay] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.TermDay].ToString());
                                t.CreatedDate = dr[recTransactionType.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recTransactionType.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recTransactionType.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recTransactionType.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.UpdatedBy].ToString();
                                t.BankID = dr[recTransactionType.Fields.BankID] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.BankID].ToString());
                                t.recBankAccount.BankName = dr[recBankAccount.Fields.BankName] == DBNull.Value ? string.Empty : dr[recBankAccount.Fields.BankName].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recTransactionType> GetListIsTariff(short isTariff)
        {
            List<recTransactionType> tt = new List<recTransactionType>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT TransTypeID, TransTypeCode, TransTypeName FROM master.recTransactionType where IsTariff = " + isTariff;
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recTransactionType t = new recTransactionType();
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
        public recTransactionType GetByLikeTransTypeName(string key)
        {
            recTransactionType t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT TransTypeID, TransTypeCode, TransTypeName, TermDay, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy, BankID FROM master.recTransactionType WHERE TransTypeName like lower('" + key + "%')";
                    cmd.CommandType = CommandType.Text;
                    //cmd.Parameters.Add("@TransTypeName", SqlDbType.VarChar).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recTransactionType();
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                t.TermDay = dr[recTransactionType.Fields.TermDay] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.TermDay].ToString());
                                t.CreatedDate = dr[recTransactionType.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recTransactionType.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recTransactionType.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recTransactionType.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.UpdatedBy].ToString();
                                t.BankID = dr[recTransactionType.Fields.BankID] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.BankID].ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public bool CheckDataByCode(Int16 code)
        {
            bool _result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat("SELECT COUNT(TransTypeCode) TransTypeCode FROM master.recTransactionType WHERE TransTypeCode=@TransTypeCode");
                    cmd.Parameters.Add("@TransTypeCode", SqlDbType.VarChar).Value = code;

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;

                    try
                    {
                        conn.Open();

                        var objId = cmd.ExecuteScalar();
                        objId = objId == DBNull.Value ? 0 : int.Parse(objId.ToString());

                        _result = int.Parse(objId.ToString()) == 0 ? false : true;

                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recTransactionType> GetListByInvoiceNoReport(string invoiceno)
        {
            List<recTransactionType> tt = new List<recTransactionType>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"SELECT DISTINCT C.TransTypeID
                                    , C.TransTypeCode
                                    , C.TransTypeName
                                    , C.TermDay
                                    , C.CreatedDate
                                    , C.CreatedBy
                                    , C.UpdatedDate
                                    , C.UpdatedBy
                                    , C.BankID
                                    , C.IsTariff 
                                    FROM transactions.recInvoice A INNER JOIN transactions.recInvoiceDetail B ON A.InvoiceNo=B.InvoiceNo
                                    INNER JOIN master.recTransactionType C ON B.TransTypeID = C.TransTypeID
                                    WHERE A.InvoiceNo=@InvoiceNo";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@InvoiceNo", SqlDbType.VarChar).Value = invoiceno;

                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recTransactionType t = new recTransactionType();
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                t.TermDay = dr[recTransactionType.Fields.TermDay] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.TermDay].ToString());
                                t.CreatedDate = dr[recTransactionType.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recTransactionType.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recTransactionType.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recTransactionType.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.UpdatedBy].ToString();
                                t.BankID = dr[recTransactionType.Fields.BankID] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.BankID].ToString());
                                t.IsTariff = dr[recTransactionType.Fields.IsTariff] == DBNull.Value ? (bool?)null : bool.Parse(dr[recTransactionType.Fields.IsTariff].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recTransactionType> GetListOnBilling() { 
        
         List<recTransactionType> tt = new List<recTransactionType>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT distinct transactions.recbilling.TransTypeID, master.recTransactionType.TransTypeCode, master.recTransactionType.TransTypeName FROM transactions.recBilling inner join master.recTransactionType on  transactions.recBilling.TransTypeID=master.recTransactionType.TransTypeID where IsBilling=1 order by TransTypeCode ASC";
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recTransactionType t = new recTransactionType();
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                //t.TermDay = dr[recTransactionType.Fields.TermDay] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.TermDay].ToString());
                                //t.CreatedDate = dr[recTransactionType.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.CreatedDate].ToString());
                                //t.CreatedBy = dr[recTransactionType.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.CreatedBy].ToString();
                                //t.UpdatedDate = dr[recTransactionType.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.UpdatedDate].ToString());
                                //t.UpdatedBy = dr[recTransactionType.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.UpdatedBy].ToString();
                                //t.BankID = dr[recTransactionType.Fields.BankID] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.BankID].ToString());
                                //t.IsTariff = dr[recTransactionType.Fields.IsTariff] == DBNull.Value ? (bool?)null : bool.Parse(dr[recTransactionType.Fields.IsTariff].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
        public List<recTransactionType> GetListIsBilling()
        {

            List<recTransactionType> tt = new List<recTransactionType>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT TransTypeID, TransTypeCode, TransTypeName FROM master.recTransactionType where IsBilling='true'";
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recTransactionType t = new recTransactionType();
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                //t.TermDay = dr[recTransactionType.Fields.TermDay] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.TermDay].ToString());
                                //t.CreatedDate = dr[recTransactionType.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.CreatedDate].ToString());
                                //t.CreatedBy = dr[recTransactionType.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.CreatedBy].ToString();
                                //t.UpdatedDate = dr[recTransactionType.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.UpdatedDate].ToString());
                                //t.UpdatedBy = dr[recTransactionType.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.UpdatedBy].ToString();
                                //t.BankID = dr[recTransactionType.Fields.BankID] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.BankID].ToString());
                                //t.IsTariff = dr[recTransactionType.Fields.IsTariff] == DBNull.Value ? (bool?)null : bool.Parse(dr[recTransactionType.Fields.IsTariff].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recTransactionType GetByTransTypeCode(short key)
        {
            recTransactionType t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT TransTypeID, TransTypeCode, TransTypeName, TermDay, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy, BankID, IsTariff, IsBilling FROM master.recTransactionType WHERE TransTypeCode = @TransTypeCode";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@TransTypeCode", SqlDbType.SmallInt).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recTransactionType();
                                t.TransTypeID = dr[recTransactionType.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeID].ToString());
                                t.TransTypeCode = dr[recTransactionType.Fields.TransTypeCode] == DBNull.Value ? (short)0 : short.Parse(dr[recTransactionType.Fields.TransTypeCode].ToString());
                                t.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();
                                t.TermDay = dr[recTransactionType.Fields.TermDay] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.TermDay].ToString());
                                t.CreatedDate = dr[recTransactionType.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recTransactionType.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recTransactionType.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recTransactionType.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recTransactionType.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.UpdatedBy].ToString();
                                t.BankID = dr[recTransactionType.Fields.BankID] == DBNull.Value ? (short?)null : short.Parse(dr[recTransactionType.Fields.BankID].ToString());
                                t.IsTariff = dr[recTransactionType.Fields.IsTariff] == DBNull.Value ? (bool?)null : bool.Parse(dr[recTransactionType.Fields.IsTariff].ToString());
                                t.IsBilling = dr[recTransactionType.Fields.IsBilling] == DBNull.Value ? (bool?)null : bool.Parse(dr[recTransactionType.Fields.IsBilling].ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }
    }

}
